package com.njust.controller;

import com.njust.common.ServerRespone;
import com.njust.form.FaultForm;
import com.njust.form.ReportFrom;
import com.njust.service.HistoryService;
import com.njust.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zjwpro@foxmail.com
 * @date 2018/3/8 下午9:25
 */
@RestController
@RequestMapping("/history")
@Slf4j
@Api("历史查询")
public class HistoryController {

    @Autowired
    private HistoryService historyService;
    @PostMapping("/report")
    @ApiOperation("报表查询")
    public ServerRespone reportQuery(@RequestBody ReportFrom reportFrom) throws ParseException {
        ServerRespone serverRespone = historyService.reportQuery(reportFrom);
        return serverRespone;
    }

    @PostMapping("/fault")
    @ApiOperation("/报警查询")
    public ServerRespone faultQuery(@RequestBody FaultForm faultForm) throws ParseException{
        //需要默认显示一个月的所有报警信息
        ServerRespone serverRespone = historyService.faultQuery(faultForm);
        return serverRespone;
    }
    @GetMapping("/optiAndAvg")
    @ApiOperation("最值及平均值")
    public ServerRespone getOptiAndAvg() throws ParseException{
        //获取当天的每辆车的最值及平均值
        Date date = new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd000000");
        String nowdate = simpleDateFormat.format(date);
        Date first = DateUtil.String2Date(nowdate);
        long time1 = first.getTime()+86400000;
        Date lasttime = new Date(time1);
        ServerRespone serverRespone = historyService.getOptiAndAvg(first,lasttime);
        return serverRespone;
    }



}
